﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace 行政区域
{
    public class XzqyNet
    {
        public XzqyNet(string webUrl)
        {
            if (!string.IsNullOrWhiteSpace(webUrl))
                WebUrl = webUrl;
        }
        /// <summary>  
        /// 网址  
        /// </summary>  
        string WebUrl = "http://www.xzqy.net/360000000000.htm";
        public List<Regional> TestSetRelationRegional()
        {
            List<Regional> strList = new List<Regional>();
            string info = "";
            if (GetPageHtml(out info))
            {
                string HTML = GetRegionalHtml(info);
                strList = GetRegionalLines(HTML);
            }
            return strList;
        }
        private const string tableExp = @"<table cellspacing=0 cellpadding=0 border=0 width=100%>.*</table>";
        private const string tdExp = "<td class=\"parent\" style=\"width:48px; \">.*</td>";
        /// <summary>  
        /// 1.获得页面全部html代码文本
        /// </summary>  
        /// <param name="info">输出字符串</param>  
        /// <returns>获取成功与否</returns>  
        private bool GetPageHtml(out string info)
        {
            try
            {
                WebClient client = new WebClient();
                client.Headers.Add("user-agent",
                                   "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

                Stream data = client.OpenRead(WebUrl);
                StreamReader reader = new StreamReader(data, Encoding.UTF8);

                //read  
                info = reader.ReadToEnd();

                data.Close();
                reader.Close();

                return true;
            }
            catch (Exception exception)
            {
                info = "发生错误:/n" + exception;
                return false;
            }
        }
        /// <summary>  
        /// 2.获得html代码文本中《县级以上行政区划》部分  
        /// </summary>  
        private string GetRegionalHtml(string pageHtml)
        {
            string text = pageHtml;
            System.Text.RegularExpressions.Match m = Regex.Match(text, tableExp);
            string ss = m.Value;
            return ss;
        }
        private List<Regional> GetRegionalLines(string regionalHtml)
        {
            List<Regional> strList = new List<Regional>();
            string[] ss = regionalHtml.Split(new string[] { "<TR>", "</TR>", "<tr>", "</tr>" },
            StringSplitOptions.RemoveEmptyEntries);

            for (int i = 1; i < ss.Length - 1; i++)
            {
                string s1 = ss[i];
                string[] s11 = s1.Split(new string[] { "<TD>", "</TD>", "<td>", "</td>" }, StringSplitOptions.RemoveEmptyEntries);

                if (s11.Length <=1)
                    continue;

                string root = s11[0];
                string child = s11[1];

                string ss_t = RegExp(root, "\">.*</a>");
                string code = RegExp(ss_t, "\\d{12}");
                string name = RegExp(ss_t, "[\u4e00-\u9fa5][\u4e00-\u9fa5]?[\u4e00-\u9fa5]?[\u4e00-\u9fa5]?[\u4e00-\u9fa5]?[\u4e00-\u9fa5]?[\u4e00-\u9fa5]?[\u4e00-\u9fa5]");

                Regional rootReg = new Regional();
                rootReg.Id = i;
                rootReg.PId = 0;
                rootReg.Code = code;
                rootReg.Name = name;

                string[] childs = child.Split(new string[] { "<a>", "</a>", "<A>", "</A>" }, StringSplitOptions.RemoveEmptyEntries);
                for (int j = 0; j < childs.Length; j++)
                {
                    ss_t = childs[j];
                    code = RegExp(ss_t, "\\d{12}");
                    name = RegExp(ss_t, "[\u4e00-\u9fa5][\u4e00-\u9fa5]?[\u4e00-\u9fa5]?[\u4e00-\u9fa5]");

                    Regional childReg = new Regional();
                    childReg.Id = j;
                    childReg.PId = i;

                    childReg.Code = code;
                    childReg.Name = name;

                    rootReg.SonRegionals.Add(childReg);
                }
                strList.Add(rootReg);
            }
            return strList;
        }
        private string RegExp(string text, string exp)
        {
            System.Text.RegularExpressions.Match m = Regex.Match(text, exp);
            string ss = m.Value;
            return ss;
        }
    }
    /// <summary>  
    /// 区域模型类  
    /// </summary>  
    public class Regional
    {
        public int Id { get; set; }
        public int PId { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
        /// <summary>  
        /// 所有子区域  
        /// </summary>  
        public List<Regional> SonRegionals
        {
            get
            {
                if (_sonregionals == null)
                    _sonregionals = new List<Regional>();
                return _sonregionals;
            }
            set { _sonregionals = value; }
        }
        private List<Regional> _sonregionals = null;
    }
}
